package com.gyms.manager;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.okhttp.beans.HVClickInfoBean;
import com.classic.okhttp.beans.HVOrderCardBean;
import com.classic.okhttp.beans.HVRefundConfigBean;
import com.classic.okhttp.enums.Enum;
import com.gyms.R;

import java.util.List;

import utils.StringUtils;


/**
 * ltx on 2016/12/8 16:36
 * 更具枚举获取订单状态
 */
public class OrderManger {
    private static final int STATUS_TEXT_COLOR_1=R.color.c_333333;
    private static final int STATUS_TEXT_COLOR_2=R.color.c_999999;

    private static final String S_FORWARD="立即前往";
    /**
     * 订单状态(1 待支付  2 已支付  3 已完成  4 退款中  5 已退款 6 已取消)
     */
    public static String getOrderStatus(int status){
        String orderStatus;
        switch (status){
            case 1:
                orderStatus="待支付";
                break;
            case 2:
            case 4:
                orderStatus="已支付";
                break;
            case 3:
                orderStatus="已完成";
                break;
            case 5:
                orderStatus="已退款";
                break;
            case 6:
                orderStatus="";
                break;
            case 7:
                orderStatus="退款失败";
                break;
            default:
                orderStatus="";
                break;
        }
        return orderStatus;
    }

    /**
     *
     * @param payway Alipay(1),	//支付宝
         Wechatpay(2);	//微信支付
     * @return 付款方式
     */
    public static String getPayWay(int payway){
        String strPayway="";
        switch (payway){
            case 1:
                strPayway="支付宝";
                break;
            case 2:
                strPayway="微信";
                break;
        }
        return strPayway;
    }

    /**
     * 是否是退款中
     * @param status 如上
     * @return true or false
     */
    public static boolean isRefunding(int status){
        boolean isRefunding=status==4||status==5;
        return isRefunding;
    }
    /**
     * 是否可以退款 只有以支付的状态才能退款
     * @param status 如上
     * @return true or false
     */

    public static boolean isCanRefund(int status){
        boolean isRefunding=status==2;
        return isRefunding;
    }

    /**
     * 是否是退款中 或者退款完成 或者完成的状态
     * @param status 如上
     * @return true or false
     */
    public static boolean isRefundOrComplete(int status){
        boolean isRefunding=status==4||status==5||status==3;
        return isRefunding;
    }

    /**
     *
     * @param isRefundings list中是否全部为退单中
     * @return true or false
     */
    public static boolean isAllRefunding(List<Boolean> isRefundings){
        boolean isAllRefunding=true;
      for(Boolean isRefunding :isRefundings){
          if(!isRefunding){
              isAllRefunding=!isAllRefunding;
              break;
          }
      }
        return isAllRefunding;
    }

    /**
     * 显示退款的状态
     * @param mImageView 显示的view
     * @param status 显示的状态
     */

    public static int showRefundStatus(ImageView mImageView, int status){
        int statusTextColor=0;
            switch (status) {
                case 1://待支付
                    mImageView.setVisibility(View.GONE);
                    statusTextColor=STATUS_TEXT_COLOR_1;
                    break;
                case 2://已支付
                    mImageView.setVisibility(View.GONE);
                    statusTextColor=STATUS_TEXT_COLOR_1;
                    break;
                case 3://已完成
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setImageResource(R.mipmap.icon_refund4);
                    statusTextColor=STATUS_TEXT_COLOR_2;
                    break;
                case 4://退款中
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setImageResource(R.mipmap.icon_refund);
                    statusTextColor=STATUS_TEXT_COLOR_2;
                    break;
                case 5://已退款
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setImageResource(R.mipmap.icon_refund2);
                    statusTextColor=STATUS_TEXT_COLOR_2;
                    break;
                case 6://已取消
                    mImageView.setVisibility(View.GONE);
                    statusTextColor=STATUS_TEXT_COLOR_2;
                    break;
                case 7://退款失败
                    mImageView.setVisibility(View.VISIBLE);
                    mImageView.setImageResource(R.mipmap.icon_refund3);
                    statusTextColor=STATUS_TEXT_COLOR_2;
                    break;
                default:

                    break;
            }
        return statusTextColor;
        }

    /**
     * @param status 订单状态 的变化布局的更新
     * @param mRlCode 验证码布局
     * @param mRlCard 卡卷
     * @param viewCardLine 卡卷的下划线
     * @param isForbiddenUseCoupon 是否禁止显示（仅正对已付款）自动核销类专用
     * @param isForbiddenBarCode 是否禁止使用优惠券（仅针对待付款）
     */

    public static void doShowOrderStatue(int status, RelativeLayout mRlCode, TextView mTvPayWay, RelativeLayout mRlCard, View viewCardLine, boolean isForbiddenUseCoupon, boolean isForbiddenBarCode) {
        switch (status) {
            case 1://待支付
                mRlCode.setVisibility(View.GONE);
                if(isForbiddenUseCoupon){//待支付的时候 隐藏优惠券
                    mRlCard.setVisibility(View.GONE);
                    viewCardLine.setVisibility(View.GONE);
                }
                mTvPayWay.setVisibility(View.GONE);
                break;
            case 2://已支付
                mTvPayWay.setVisibility(View.VISIBLE);
                if(isForbiddenBarCode){
                    mRlCode.setVisibility(View.GONE);
                }else {
                    mRlCode.setVisibility(View.VISIBLE);
                }
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            case 3://已完成
                if(isForbiddenBarCode){
                    mRlCode.setVisibility(View.GONE);
                }else {
                    mRlCode.setVisibility(View.VISIBLE);
                }
                mTvPayWay.setVisibility(View.VISIBLE);
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            case 4://退款中
                if(isForbiddenBarCode){
                    mRlCode.setVisibility(View.GONE);
                }else {
                    mRlCode.setVisibility(View.VISIBLE);
                }
                mTvPayWay.setVisibility(View.VISIBLE);
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            case 5://已退款
                mRlCode.setVisibility(View.GONE);
                mTvPayWay.setVisibility(View.VISIBLE);
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            case 6://已取消
                mRlCode.setVisibility(View.GONE);
                mTvPayWay.setVisibility(View.GONE);
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            case 7://退款失败
                if(isForbiddenBarCode){
                    mRlCode.setVisibility(View.GONE);
                }else {
                    mRlCode.setVisibility(View.VISIBLE);
                }
                mTvPayWay.setVisibility(View.VISIBLE);
                mRlCard.setVisibility(View.VISIBLE);
                viewCardLine.setVisibility(View.VISIBLE);
                break;
            default:


                break;
        }
    }





        /**
         * 获取实际支付的价格
         * @param mAllPrice 总价
         * @param mHVMyOrderInfoBeanCard 优惠劵bean
         * @return 实付
         */

        public static double getActualPrice(double mAllPrice,HVOrderCardBean mHVMyOrderInfoBeanCard){
            double mActualPrice=mAllPrice;//总价
            if(!StringUtils.isNull(mHVMyOrderInfoBeanCard)){//卡卷不为null
                double mDiscountedPrice=mHVMyOrderInfoBeanCard.getCouponMoney();//卡券优惠价格
                mActualPrice=mAllPrice-mDiscountedPrice;//实付
            }
            return mActualPrice;
        }

    /**
     * 在未支付的情况下 优惠卷
     * @param mAvailableCouponCount 可用优惠价数量
     * @param mCardCountView view
     */

    public static void setAvailableCouponCount(Context mContext, Integer mAvailableCouponCount, TextView mCardCountView){
        if(null==mAvailableCouponCount||0==mAvailableCouponCount){
            mCardCountView.setTextColor(mContext.getApplicationContext().getResources().getColor(R.color.c_999999));
            mCardCountView.setText(0==mAvailableCouponCount?"无可用":"请选择");
        }else{
            mCardCountView.setTextColor(mContext.getApplicationContext().getResources().getColor(R.color.c_FF9900));
            mCardCountView.setText(mAvailableCouponCount+"张可用");
        }
    }

    /**
     *
     * @param mConfigBean 退款规则
     * @param tvRevertRule view
     */

    public static void doShowRevertText(HVRefundConfigBean mConfigBean, TextView tvRevertRule){
        int time=mConfigBean.getDueTime();//秒
        tvRevertRule.setText("提前"+getTimeFromS(time)+"可退款");
    }

    public static String getTimeFromS(int time){
        String mStrTime="";
        if(time<3600){//一个小时内显示分
            mStrTime=time/60+"分钟";
        }else if(time>=3600&&time<86400){//小时为单位
            mStrTime=time/3600+"小时"+(time%3600)/60+"分钟";
        }else {//天为单位
            mStrTime=time/86400+"天"+(time%86400)/3600+"小时";
        }

        return mStrTime;
    }

    /**
     * 立即前往显示逻辑
     * @param orderStaus 状态
     * @param mForwardDetail mForwardDetail
     * @param llForward 立即前往布局
     * @param mForwardButtonText 立即前往的文案
     */

    public static void doForward(int orderStaus, HVClickInfoBean mForwardDetail, LinearLayout llForward, String mForwardButtonText){
        if(null==mForwardDetail){
            llForward.setVisibility(View.GONE);
            return;
        }
        String mForwardText=StringUtils.isNull(mForwardButtonText)?S_FORWARD:mForwardButtonText;
        TextView mTvForward= (TextView) llForward.getChildAt(0);
        switch (orderStaus) {
            case 1://待支付
                llForward.setVisibility(View.GONE);
                break;
            case 2://已支付
                llForward.setVisibility(View.VISIBLE);
                break;
            case 3://已完成
                llForward.setVisibility(View.VISIBLE);
                break;
            case 4://退款中
                llForward.setVisibility(View.VISIBLE);
                break;
            case 5://已退款
                llForward.setVisibility(View.GONE);
                break;
            case 6://已取消
                llForward.setVisibility(View.GONE);
                break;
            case 7://退款失败
                llForward.setVisibility(View.VISIBLE);
                break;
            default:
                llForward.setVisibility(View.GONE);
                break;
        }
        if(mTvForward.getVisibility()==View.VISIBLE){
            mTvForward.setText(mForwardText);
        }
    }

    /**
     * 点击前进
     * @param mForwardDetail mForwardDetail
     * @param mBannerIntentManger mBannerIntentManger
     */

    public static void goForwardClick(HVClickInfoBean mForwardDetail, BannerIntentManger mBannerIntentManger){
        Enum.EnumHomepageAction action = mForwardDetail.getAction();
        if(!StringUtils.isNull(mForwardDetail.getUrl())) {
            String[] gymIds = mForwardDetail.getUrl().split(";");
            if(gymIds.length > 0) {
                mBannerIntentManger.bannerIntent(mForwardDetail,action,mForwardDetail.getTitle(), gymIds);
            }
        }
    }
}
